package com.hu.common.util.adm;


import com.hu.common.enums.MessageEnum;
import com.hu.common.exception.BizException;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.Files;

/**
 * 通用响应对象
 * @author 355760
 */
public class Response<T> implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    /**
     * 响应码
     */
    private Integer code;
    
    /**
     * 响应信息
     */
    private String msg;
    
    /**
     * 响应内容
     */
    private T data;
    
    public static <T> Response<?> OK() {
        return new Response<T>(MessageEnum.SUCCESS.getCode(), MessageEnum.SUCCESS.getMessage());
    }

    @SuppressWarnings("unchecked")
    public static <T> Response<T> OK(T data) {
        Response<T> response = (Response<T>) OK();
        response.setData(data);
        return response;
    }

    public static Response<String> Fail() {
        return new Response<>(MessageEnum.FAIL.getCode(), MessageEnum.SUCCESS.getMessage());
    }

    public static Response<String> EXCEPTION() {
        return new Response<>(MessageEnum.EXCEPTION.getCode(), MessageEnum.EXCEPTION.getMessage());
    }

    public static Response<String> FROM_ENUM(MessageEnum respMessageEnum) {
        return new Response<>(respMessageEnum.getCode(), respMessageEnum.getMessage());
    }

    public static Response<String> Fail(String message) {
        Response<String> response = Response.Fail();
        response.setMsg(message);
        return response;
    }

    public Response() {
    }
    
    public Response(Integer code, String message) {
        this.code = code;
        this.msg = message;
    }
    
    public Response(Integer code, String message, T data) {
        this.code = code;
        this.msg = message;
        this.data = data;
    }
    
    public boolean isSuccess() {
        return !ObjectUtils.isEmpty(code) && code == 200;
    }
    
    public Integer getCode() {
        return code;
    }
    
    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }

    public void sendFile(HttpServletResponse response, String filePath) {
        File file = new File(filePath);
        response.setHeader("content-type", "application/octet-stream");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment; filename=" + file.getName());
        response.setHeader("Access-Control-Expose-Headers","content-disposition");
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        OutputStream os;

        try {
            os = response.getOutputStream();
            bis = new BufferedInputStream(Files.newInputStream(file.toPath()));
            int i = bis.read(buff);

            while (i != -1) {
                os.write(buff, 0, buff.length);
                os.flush();
                i = bis.read(buff);
            }
        } catch (Exception e) {
            throw new BizException(MessageEnum.EXCEPTION.getMessage());
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    @Override
    public String toString() {
        return "Response{" + "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
